package net.tplusplus.drools.speedtest.facts;

import java.util.Date;

import net.tplusplus.drools.speedtest.bank.Account;
import net.tplusplus.rules.runner.rao.Fact;


public class Cashflow implements Fact, Comparable<Cashflow>
{	
	public static final int CREDIT = 0;
	public static final int DEBIT = 1;
	
	private Date date;
	private long datel;
	private Long dateL;
	private double amount;
	private int type;
	private Account account;
	
	public Cashflow()
	{
	}
	
	public Cashflow(Date date, double amount, int type, Account account)
	{
		this();
		
		this.date = date;
		this.datel= date.getTime();
		this.dateL = date.getTime();
		this.amount = amount;
		this.type = type;
		this.account = account;
	}

	public Date getDate()
	{
		return date;
	}
	
	public void setDate(Date date)
	{
		this.date = date;
	}
	
	public double getAmount()
	{
		return amount;
	}
	
	public void setAmount(double amount)
	{
		this.amount = amount;
	}
	
	public int getType()
	{
		return type;
	}

	public void setType(int type)
	{
		this.type = type;
	}

	public Account getAccount()
	{
		return account;
	}

	public void setAccount(Account account)
	{
		this.account = account;
	}
	
	public long getDatel()
	{
		return datel;
	}
	
	public long getDateL()
	{
		return dateL;
	}
	
	/**
	 *  This method enables the Collections api to sort the 
	 *  Cashflow by cashflow date.
	 */	
	public int compareTo(Cashflow cashflow)
	{
		if(getDate().after(cashflow.getDate()))
		{
			return 1;
		}
		if(getDate().before(cashflow.getDate()))
		{
			return -1;
		}	
		
		return 0;
	}
	
	/**
	 *  This method enables the comparison of Cashflow objects
	 */	
	@Override
	public boolean equals(Object obj) 
	{
		if(obj==null || !(obj instanceof Cashflow))
		{
			return false;
		}
		
		Cashflow cashflow = (Cashflow) obj;
		
		if(!getAccount().equals(cashflow.getAccount())) return false;
		if(getAmount()!=cashflow.getAmount()) return false;
		if(!getDate().equals(cashflow.getDate())) return false;
		if(getType()!=cashflow.getType()) return false;
		
		return true;
	}	
	
	public String toString()
	{
		return "Cashflow["
				//+"account="+account
				+"date="+date
				+",amount="+amount
				+",type="+(type==CREDIT?"Credit":"Debit")
				+"]";
	}
	
	
}
